package sw6.launcher;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import sw6.launcher.TableInfo.Applications;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Adapter for adding SSIDs to the blacklist in the SSIDBlack activity.
 * @author SW6C
 *
 */
public class SSIDAdapter extends BaseAdapter {
        private final String tag = "GIRAF";
	
        private ArrayList<String> SSIDS = new ArrayList<String>();
        private Context mContext;
        private Uri dataUri;
        
        @SuppressWarnings("unchecked")
		public SSIDAdapter(Uri data, Context context) {
                this.dataUri = data;
                this.mContext = context;
                
                
                notifyDataSetInvalidated();
                String[] projection = {Applications.SSID_BLACKLIST};
                Cursor cursor = mContext.getContentResolver().query(dataUri, projection, null, null, null);
                
                if(cursor.moveToFirst()) {
                	try{
                		Log.d(tag,Integer.toString(cursor.getColumnIndex(Applications.SSID_BLACKLIST)));
                		ByteArrayInputStream bis = new ByteArrayInputStream (cursor.getBlob(cursor.getColumnIndex(Applications.SSID_BLACKLIST)));
                		ObjectInputStream ois = new ObjectInputStream (bis);
                		this.SSIDS = (ArrayList<String>) ois.readObject();
                	} catch (IOException e) {
                		Toast.makeText(mContext, "There was an error getting the list. Please report this message: " + e.getMessage(), Toast.LENGTH_LONG).show();
                	} catch (ClassNotFoundException e) {
                		Log.e(tag,"ArrayList was not found!");
                	}
                }
                cursor.close();
				notifyDataSetChanged();
                
        }

        @Override
        public int getCount() {
                return SSIDS.size();
        }

        @Override
        public Object getItem(int position) {
                return SSIDS.get(position);
        }

        @Override
        public long getItemId(int arg0) {
                return arg0;
        }
        
        /**
         * Adds the ssid parameter to the SSIDS list for blacklisting.
         * @param ssid
         * 			The SSID to be blacklisted.
         */
        public void additem(String ssid){
                this.SSIDS.add(ssid);
                this.notifyDataSetChanged();
        }
        
        /**
         * Inserts a new string to a given position in the SSIDS list.
         * @param id
         * 			The position in the list to be edited.
         * @param ssid
         * 			The string to be added.
         */
        public void edititem(int id, String ssid){
                this.SSIDS.set(id, ssid);
                this.notifyDataSetChanged();
        }
        
        /**
         * Deletes a string from the SSIDS list.
         * @param id
         * 			The position in the list to be deleted.
         */
        public void deleteitem(int id){
                this.SSIDS.remove(id);
                this.notifyDataSetChanged();
        }
        

        @Override
        public void notifyDataSetChanged() {
        	try{
        		ContentValues values = new ContentValues();
        		
        		ByteArrayOutputStream stream = new ByteArrayOutputStream();
        		ObjectOutputStream out = new ObjectOutputStream( stream );

        		out.writeObject(SSIDS);
        		out.flush();

        		values.put(Applications.SSID_BLACKLIST, stream.toByteArray());

        		stream.close();
        		out.close();
        		
        		mContext.getContentResolver().update(dataUri, values, null, null);
        	} catch (Exception e) {
        		Toast.makeText(mContext, "There was an error updating the changes. Please report this: " + e.getMessage(), Toast.LENGTH_LONG).show();
        		Log.e(tag,e.getMessage());
        	}
        	

        	super.notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        	TextView textView;
        	if (convertView == null) {  // if it's not recycled, initialize some attributes
        		textView = new TextView(parent.getContext());        
        	} else {
        		textView = (TextView) convertView;
        	}
        	textView.setTextSize(20);
        	textView.setText(SSIDS.get(position));
        	return textView;
        }
}